<?php

namespace app\api\controller;

use app\api\model\wanlshop\Coupon;
use app\api\model\wanlshop\CouponReceive;
use app\common\controller\Api;

use app\admin\model\User;
use think\Db;

/**
 *  定时任务
 */
class Task extends Api
{

    protected $noNeedLogin = ['coupon'];
    protected $noNeedRight = '*';
    protected $model ='';
    public function _initialize()
    {
        parent::_initialize();
    }
    //定时任务 - 优惠卷过期自动关闭
    public function coupon()
    {
        $model =  new CouponReceive();
        //用于跑数据的
//        $list =  $model->where(['state'=>1])->select();
//        if($list){
//            foreach ($list as $v){
//                    if($v['pretype'] == 'fixed'){
//                        $v->endtime = strtotime($v['enddate']) + 86399;
//                        $v->save();
//                    }
//
//                if($v['pretype'] == 'appoint' && $v['validity'] >0){
//                    $v->endtime = strtotime($v['validity'].' day',strtotime($v['enddate'])+86399);
//                    $v->save();
//                }
//            }
//        }
//        var_dump($list); exit;

        $list =  $model->where(['state'=>1])->where('endtime','<>',0)->where('endtime','<=',time()+60*30)->select();
        if($list){
            foreach ($list as $v){
              if($v['endtime']  <= time()){
                  $v->state = 3;
                  $v->save();
              }else{
                  $delay = $v['endtime']- time();
                  \think\Queue::later($delay, '\addons\wanlshop\job\OrderAutoOper@couponClose', $v, 'wanlshop');
              }
              
            }
        }
        echo '执行成功';
    }

    //定时任务 - 会员过期自动关闭
    public function memberExpire()
    {
        $model =  new User();
        $list =  $model->where('level','>',1)->where('member_expire_time','<>',0)->where('member_expire_time','<=',time()+60*30)->select();
        if($list){
            foreach ($list as $v){
                if($v['member_expire_time']  <= time()){
                    $v->level = 1;
                    $v->member_expire_time = -1;
                    $v->save();
                }else{
                    $delay = $v['member_expire_time']- time();
                    \think\Queue::later($delay, '\addons\wanlshop\job\OrderAutoOper@memberClose', $v, 'wanlshop');
                }

            }
        }
    }
}
